/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cdp.web.component.metadata.initializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.inspur.edp.cdp.cdf.component.manager.initializer.ComponentInitializer;
import com.inspur.edp.cdp.common.utils.json.JsonUtil;
import com.inspur.edp.cdp.web.component.metadata.ComponentConstant;
import com.inspur.edp.cdp.web.component.metadata.define.WebComponentMetadata;
import com.inspur.edp.component.schema.AbstractComponent;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import org.apache.commons.lang3.StringUtils;

/**
 * 功能描述:
 *
 * @ClassName: Initializer
 * @Author: Fynn Qi
 * @Date: 2020/12/29 17:49
 * @Version: V1.0
 */
public class Initializer extends ComponentInitializer {

    @Override
    protected void initInfo(GspMetadata metadata, AbstractComponent component) {
        WebComponentMetadata webComponentMetadata = (WebComponentMetadata) component;
        if (StringUtils.isNotBlank(metadata.getExtendProperty())) {
            JsonNode jObj = JsonUtil.toJsonNode(metadata.getExtendProperty());
            boolean isCommon = jObj.get(ComponentConstant.metadataExtendPropertyField.IS_COMMON) != null ?
                    JsonUtil.toObject(jObj.get(ComponentConstant.metadataExtendPropertyField.IS_COMMON), boolean.class) : true;
            webComponentMetadata.setCommon(isCommon);
            String formCode = jObj.get(ComponentConstant.metadataExtendPropertyField.FORM_CODE) != null ?
                    JsonUtil.toObject(jObj.get(ComponentConstant.metadataExtendPropertyField.FORM_CODE), String.class) : null;
            webComponentMetadata.setFormCode(formCode);
        } else {
            webComponentMetadata.setCommon(true);
        }
    }

    @Override
    protected AbstractComponent initComponent() {
        return new WebComponentMetadata();
    }
}
